import Vue from 'vue';
import VueRouter from 'vue-router';
import UserLogin from '@/pages/UserLogin.vue'; // 确保路径正确
import SaveUser from '@/pages/SaveUser.vue'
import AdminLogin from '@/pages/admin/AdminLogin.vue';
import BlueLotusDetail from '../views/BlueLotusDetail.vue'
import PeonyDetail from '../views/PeonyDetail.vue'

Vue.use(VueRouter);

const routes = [
  {
    // 用户登录
    path: '/login',
    name: 'Login',
    component: UserLogin,
  },
  {
    //管理员登录
    path: '/adminlogin',
    name: 'adminLogin',
    component: AdminLogin,
  },
  {
    //管理员登录新路径 - 添加这个路径以匹配UserHeader中的跳转
    path: '/admin/login',
    name: 'AdminLoginPage',
    component: AdminLogin,
  },
  {
    //用户注册
    path: '/saveuser',
    name: 'SaveUser',
    component: SaveUser,
  },
  {
    //管理员注册
    path: '/saveadmin',
    name: 'SaveAdmin',
    component: SaveUser,
  },
  {
    //订单结算页面
    path: '/balance',
    component: () => import('../views/UserBalance.vue')
  },
  {
    //帖子详情页
    path: '/detail/:id',
    name: 'PostDetail',
    component: () => import('../views/UserPostDetail.vue'),
    props: true
  },
  {
    //支付页面
    path: '/payment',
    name: 'Payment',
    component: () => import('@/views/PaymentPage.vue')
  },
  {
    //用户界面
    path: '/',
    name: 'UserManage',
    component: () => import('@/pages/UserManage.vue'),
    redirect: '/home', // 默认重定向到子路由
    children: [
      {
        //用户首页
        path: 'home',
        component: () => import('../views/UserHome.vue')
      },
      {
        //用户商城页
        path: 'new',
        component: () => import('../views/UserNew.vue')
      },
      {
        //用户订单页
        path: 'payments',
        component: () => import('../views/UserPayments.vue')
      },
      {
        path: 'forum',
        component: () => import('../views/UserForum.vue')
      },
      {
        path: 'events',
        component: () => import('../views/UserEvents.vue')
      },
      {
        //用户好友页
        path: 'friends',
        component: () => import('../views/UserFriends.vue')
      },
      {
        //地址管理
        path: 'address',
        name: 'UserAddress',
        component: () => import('../views/UserAddress.vue')
      },
      {
        //用户私信详情
        path: 'chat/:id',
        name: 'UserChat', // 给路由命名有助于编程式导航
        component: () => import('../views/UserChat.vue'),
        props: true, // 确保这里设置为 true
      },
      {
        //用户收藏列表
        path: 'favorites',
        name: 'UserFavorites', // 给路由命名有助于编程式导航
        component: () => import('../views/UserFavorites.vue'),
        props: true, // 确保这里设置为 true
      },
      {
        //用户个人信息页
        path: 'profile',
        component: () => import('../views/UserProfile.vue')
      },
      {
        //用户他人信息页
        path: 'otherprofile/:id',
        component: () => import('../views/UserOtherProfile.vue')
      },
      {
        //活动
        path: 'activity',
        component: () => import('../views/UserActivity.vue')
      },
      {
        //用户学习
        path: 'settings',
        component: () => import('../views/UserSettings.vue')
      },
      {
        //用户购物车页面
        path: 'mycart/:id',
        component: () => import('../views/UserMycart.vue'),
        props: true, // 确保这里设置为true
      },
      {
        //花种详细信息
        path: 'flowersdetail/:id', // 注意这里的路径格式
        component: () => import('../views/UserFlowersDetail.vue'), // 帖子详细页
        props: true, // 确保这里设置为true
      },
      {
        path: '/orders',
        name: 'OrderList',
        component: () => import('../views/OrderList.vue'),
        meta: {
          requiresAuth: true,
          title: '我的订单'
        }
      },
      {
        //花卉详情页面
        path: 'flower-detail/:id',
        name: 'FlowerDetail',
        component: () => import('@/views/FlowerDetail.vue')
      },
      {
        path: 'logistics',
        name: 'LogisticsDemo',
        component: () => import('../views/Logistics.vue'),
        meta: {
          title: '物流跟踪'
        }
      },
    ]
  },
  {
    //管理员界面
    path: '/admin',
    name: 'AdminManage',
    component: () => import('@/pages/admin/AdminManage.vue'),
    redirect: '/adminhome', // 默认重定向到子路由
    children: [
      {
        // 管理员首页
        path: '/adminhome',
        component: () => import('../views/admin/AdminHome.vue')
      },
      {
        // 荣誉管理
        path: '/adminhonor',
        component: () => import('../views/admin/AdminHonor.vue')
      },
      {
        // 用户管理
        path: '/adminuser',
        component: () => import('../views/admin/AdminUser.vue')
      },
      {
        // 活动管理
        path: '/adminactivity',
        component: () => import('../views/admin/AdminActivity.vue')
      },
      {
        // 首页设定
        path: '/adminportal',
        component: () => import('../views/admin/AdminPortal.vue')
      },
      {
        // 花种管理
        path: '/adminseeds',
        component: () => import('../views/admin/AdminSeeds.vue')
      },
      {
        // 工具管理
        path: '/admintools',
        component: () => import('../views/admin/AdminTools.vue')
      },
      {
        // 公告管理
        path: '/announcement',
        component: () => import('../views/admin/AdminAnnouncements.vue')
      },
      {
        // 订单详情页面
        path: '/order/detail/:id',
        name: 'AdminOrderDetail',
        component: () => import('../views/admin/AdminOrderDetail.vue'),
        props: true
      }
    ]
  },
  {
    path: '/flower-detail/:id',
    name: 'FlowerDetail',
    component: () => import('@/views/FlowerDetail.vue')
  },
  {
    path: '/orders',
    name: 'OrderList',
    component: () => import('@/views/OrderList.vue'),
    meta: {
      requiresAuth: true,
      title: '我的订单'
    }
  },
  {
    path: '/flower/blue-lotus',
    name: 'BlueLotusDetail',
    component: BlueLotusDetail
  },
  {
    path: '/flower/peony',
    name: 'PeonyDetail',
    component: PeonyDetail
  }
];

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes,
});

// Add error handler for navigation duplicated
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => {
    if (err.name !== 'NavigationDuplicated') {
      throw err;
    }
  });
};

export default router;